#include "serveur_multithread.h"


using namespace std;



int main (int argc,char* argv[]){

cout << "Serveur demarré...\n";

cout << "  .-.     .-.     .-.             .-.     .-.     .-. \n";
cout << ".'   `._.'   `._.'   `. SERVEUR .'   `._.'   `._.'   `._ \n\n";


param parmesan;
memPetS flex;
flex=newMemoirePartagee();
parmesan.mem=&flex;


try{
	// Creation du socket du serveur
    ServerSocket server(30001);

	parmesan.server=&server;
	
	while(true){
		
		pthread_t go;
		

		
		//TODO Verrouiller le verrou des param
		pthread_create(&go,NULL,connexionClient,(void*)&parmesan);
		//pthread_join(&go,NULL); //NULL, pointeur pour recuperer la valeur de pthread_exit(pointeur);
		

	}
}catch ( SocketException& e ){
	cout << "Exception was caught: " << e.description();
}

destructMemPartagee(&flex);
cout << "Arret du serveur.\n";
return 0;
}

/**Thread de connexion d'un client

Doit recuperer la new_sock du serveur.
**/
void* connexionClient(void* parametre){

int menu=-1;
int nb=-1;
string id_client="inconnu";
string login,mdp;
string reply="";
bool deconnect=false;

param* parmesan=(param*) parametre;
//TODO deverouiller le verrou des param
ServerSocket new_sock;
parmesan->server->accept(new_sock);

/*
	try{
	   	//while(true){
		  	string data;
		  	new_sock >> data;
		  	cout << "Le client a envoye : \n\"" << data << "\", recu par le thread : " << pthread_self() << "\"\n";
			new_sock << "Message bien recu";
		//}
	}catch(SocketException& e) {
		cout << "Exception was caught: " << e.description() << "\nArret d'urgence :)\n";
	}
*/	
	try{
		while(true){
			menu=0;
		
			if(!deconnect){//Si un client ne vient pas de se deco
				cout << "\nAttente de la reception du choix..."<<endl;
			}

			
			//new_sock << "Menu...";
			
			new_sock >> menu;
			new_sock << "ok";
			cout << menu <<endl;
			deconnect=false;
			
			switch(menu){
			
				case 0:
					//cout << "Attente de la reception du login..."<<endl;
					new_sock >> id_client;
					cout << "Deconnexion du client : " << id_client << endl << endl;
					deconnect=true;
					pthread_exit(NULL);
				break;
			
				case 1:
					//cout << "Recu : " << menu <<endl;
					
					new_sock >> login;
					//new_sock << "ok";
					new_sock >> mdp;
					cout << "Login : " << login <<endl<< "Mdp : " << mdp <<endl;
					
					new_sock << -1;
					//new_sock >> reply;
					/*
					if(isValid(login,mdp){
						//Connexion
					}else{
						new_sock << -1;
					}
					*/
				break;
				
				case 2:
					new_sock >> nb;
				
					cout << "Recu : " << menu <<endl;
					cout << "Recu : " << nb <<endl;
					//cout << "Yes!! \n";
					//creationCompte();
				break;
				
				case 4: //Utilisation de la memoire partagee
					new_sock << parmesan->mem->idMP;
					
				break;
				
				default:
					cout << "Donnee inconnue, mauvaise transmission?" <<endl;
					//new_sock >> nb;
				
					cout << "Recu : " << menu <<endl;
					cout << "Recu : " << nb <<endl;
				break;
			}
		}	
	}catch(SocketException& e) {
		if(!deconnect){//Si un client ne vient pas de se deco
			cout << "Exception was caught: " << e.description() << "\nArret d'urgence :)\n";
		}
	}

}



memPetS newMemoirePartagee(){

memPetS flex;

//Creation de la memoire partagee
key_t laCle=ftok("fichierPourCreerLaMemoire",5);

int idM=shmget(laCle,(size_t)5 * sizeof(int),IPC_CREAT|0666);

printf("Identifiant memoire : %d\n",idM);

//Creation du semaphore
key_t laCleS=ftok("fichierPourCreerLaMemoire",8);
 
int idS=semget(laCleS,1,IPC_CREAT|0666);
printf("Identifiant semaphore : %d\n",idS);

semctl(idS,0,SETVAL,1); //Initialisation du sémapore à 1

flex.idMP=idM;
flex.idSem=idS;



return flex;

}

int destructMemPartagee(memPetS* flex){

//Destruction de la memoire partagee

int res=shmctl(flex->idMP,IPC_RMID,NULL);
flex->idMP=-1;
//"Connexion" du semaphore

//printf("Identifiant semaphore : %d\n",flex.idSem);

res=semctl(flex->idSem,0,IPC_RMID); //Destruction du sémaphore
flex->idSem=-1;
printf("C'est detruit!\n");

return 0;
}



